package org.tigr.htc.request;

/*
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

import org.apache.log4j.Logger;

/**
 * The <b>RequestFactory</b> class is used to create a new IRequest. 
 * You can create a new request by calling newRequest. The method newRequest also
 * can take a string that contains the path to a java properties file to override
 * the default configuration of the request.  The class also checks the system property
 * "htc.request.config" for the location of a properties files. The order of precedence
 * of property files:
 *   1. properties file passed as a system property (-D htc.request.config="path").
 *   2. properties file in defaultConfig.
 * 
 */
public class RequestFactory {
    static Logger log = Logger.getLogger(RequestFactory.class);
    
    //
    // static class no public constructor
    //
    private RequestFactory() { }
    
    /**
     * <code>newRequest</code> create a new IRequest object. It uses the the default
     * configuration file if the system property "htc.request.config" is not set.
     * 
     * @return an <code>IRequest</code> a new object
     * @exception Exception
     *                if an error occurs
     */
    public static IRequest newRequest() throws Exception {
        return createRequest();
    }
    
    /**
     * <code>newRequest</code> create a new IRequest object. It uses the the default
     * configuration file if the system property "htc.request.config" is not set.
     * 
     * @param IRequest
     * @return an <code>IRequest</code> a new object
     * @exception Exception
     *                if an error occurs
     */
    public static IRequest newRequest(IRequest p_request) throws Exception {
        return (IRequest) new HTCRequest(p_request);
    }
    
    /**
     * <code>reconnect</code> to a request that was created and submitted earlier.
     * Takes the unique id of a request and looks up that request and returns a new
     * IRequest for that request.
     * 
     * @param id a <code>long</code> value
     * @return an <code>IRequest</code> value
     * @exception Exception
     *                if an error occurs
     */
    public static IRequest reconnect(long id) throws Exception {
        return createRequest(id);
    }
    
    //
    // call the actual constructor of the correct request class
    //
    private static IRequest createRequest(long p_id) throws Exception {
        return (IRequest) new HTCRequest(p_id);
    }
    
    //
    // call the actual constructor of the correct request class
    //
    private static IRequest createRequest() throws Exception {
        return (IRequest) new HTCRequest();
    }
    
}
